<template>
	<view>
		<view class="uni-header">
			<uni-stat-breadcrumb class="uni-stat-breadcrumb-on-phone" />
		  <view class="uni-group">
		    <view class="uni-title"></view>
		    <view class="uni-sub-title"></view>
		  </view>
		  <view class="example-body">
		  			<uni-datetime-picker
		  				v-model="range"
		  				type="daterange"
						@change="searchByDate"
						return-type="timestamp"
		  				rangeSeparator="至"
		  			/>
		  		</view>
		  <view class="uni-group">
		    <input class="uni-search" type="text" v-model="query" @confirm="search" placeholder="请输入搜索内容" />
		    <button class="uni-button" type="default" size="mini" @click="search">搜索</button>
		  </view>
		</view>
		<view style="padding:30px;">
		
				<uni-table border>
					<uni-tr>
						<uni-th>
							 日期
						</uni-th>
						<uni-th>
							姓名
						</uni-th>
						<uni-th>
							数量
						</uni-th>
					</uni-tr>
					<uni-tr v-for="(item,index) in data">
						<uni-td>
							今日
						</uni-td>
						<uni-td>
							{{item.nickname}}
						</uni-td>
						<uni-td>
							{{item._id.goods.length}}
						</uni-td>
					</uni-tr>
				</uni-table>
			</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				data:[],
				range:[]
			}
		},
		onLoad() {
			//查询出管理员用户列表
			const db = uniCloud.database();
			const dbCmd = db.command;
			var that = this;
			//当天开始时间
			var curStartTime=()=>{let time = new Date(new Date().setHours(0, 0, 0, 0)).getTime();return time;
			}
			var startTime = curStartTime();
			//当天结束时间
			var curEndTime=()=>{
				const time = new Date(new Date().setHours(23,59,59,999)).getTime();
				return time;
			}
			var endTime = curEndTime();
			const users = db.collection('uni-id-users').where("role=='administrators' || role=='admin' || role=='region'").field('_id,username,nickname,role').getTemp();
			const goods = db.collection('goods').where(dbCmd.and({
				create_date:dbCmd.lt(endTime),
				create_date:dbCmd.gt(startTime)
			})).getTemp({
				getCount:true
			});
			db.collection(users,goods).get().then(res=>{
				this.data = res.result.data;
			});
		},
		methods: {
			
			searchByDate(date){
				const db = uniCloud.database(); 
				const dbCmd = db.command;
				const users = db.collection('uni-id-users').where("role=='administrators' || role=='admin'").field('_id,username,nickname,role').getTemp();
				const goods = db.collection('goods').where(dbCmd.and({
					create_date:dbCmd.lt(date[1]),
					create_date:dbCmd.gt(date[0])
					
				})).getTemp({
					getCount:true
				});
				db.collection(users,goods).get().then(res=>{
					this.data = res.result.data;
				});
			}
		}
	}
</script>

<style>

</style>
